import 'login_model.dart';

/**
 * 标准的 dart 模型类
 */
class ResponseResult<T> {
  //#region 状态变量
  bool? success;
  T? data;
  String? msg;
  int? code;
  //#endregion
  //#region 构造方法
  ResponseResult({this.success,this.data,this.msg,this.code});
  //#endregion
  //#region fromJson
  factory ResponseResult.fromJson(Map<String,dynamic> json){
    T? data;
    //#region 反序列化 data
    if (json['data'] != null) {
      // 根据T的类型反序列化data
      if (T == LoginModel) {
        data = LoginModel.fromJson(json['data']) as T;
      }else if( T == String) {
        data = json['data'] as T;
      }
      // 可以添加其他类型的处理
    }
    //#endregion
    return ResponseResult<T>(
      success: json['success'],
      data: data,
      msg: json['msg'],
      code: json['code'],
    );
  }
  //#endregion
  //#region empty
  factory ResponseResult.empty(){
    return ResponseResult<T>(
      success: false,
      msg: 'empty',
      code: -1,
    );
  }
  //#endregion
  //#region toJson
  Map<String,dynamic> toJson(){
    final Map<String,dynamic> jsonData = <String,dynamic>{};
    jsonData['success'] = success;
    jsonData['data'] = data;
    jsonData['msg'] = msg;
    jsonData['code'] = code;
    return jsonData;
  }
  //#endregion
}